<?php

class SmsService
{

    const random_num = 6;//验证码长度
    /*
     * 是否是合适的sms type
     *
     */

    public static function isVaildType($type,$mobile,$apptype)
    {

        //用户端

        if($apptype == MsgConst::APP_TYPE_USER)
        {

            //用户注册验证短信
            if($type == MsgConst::SMS_TYPE_REGISTER)
            {
                //检查改手机号是否已经注册
                $user = UserService::getUserByMobile($mobile);

                if(empty($user)==false)
                    throw new Exception("该手机号已经注册");

                return;
            }

            //用户登录验证短信
            if($type == MsgConst::SMS_TYPE_LOGIN)
            {
                //检查改手机号是否已经注册
                $user = UserService::getUserByMobile($mobile);

                if(empty($user)==true)
                    throw new Exception("该手机号未注册");

                return;
            }

            //用户重置密码验证短信
            if($type == MsgConst::SMS_TYPE_RESET)
            {
                //检查改手机号是否已经注册
                $user = UserService::getUserByMobile($mobile);

                if(empty($user)==true)
                    throw new Exception("该手机号未注册");

                return;
            }


        }


        //工人端
        if($apptype == MsgConst::APP_TYPE_WORKER)
        {
            //工人注册验证短信
            if($type == MsgConst::SMS_TYPE_REGISTER_WORKER)
            {
                //检查改手机号是否已经注册
                $worker =WorkerService::getWorkerByMobile($mobile);

                if(empty($worker)==false)
                    throw new Exception("该手机号已经注册");

                return;
            }

            //工人登录验证短信
            if($type == MsgConst::SMS_TYPE_LOGIN_WORKER)
            {
                //检查改手机号是否已经注册
                $worker =WorkerService::getWorkerByMobile($mobile);

                if(empty($worker)==true)
                    throw new Exception("该手机号未注册");

                return;
            }

            //工人重置密码验证短信
            if($type == MsgConst::SMS_TYPE_RESET_WORKER)
            {
                //检查改手机号是否已经注册
                $worker =WorkerService::getWorkerByMobile($mobile);

                if(empty($worker)==true)
                    throw new Exception("该手机号未注册");

                return;
            }

        }


        //验证短信类型错误
        throw new Exception("验证短信类型错误");




    }

    /*
     *
     * 获取随机数
     *
     */
    public static function getRandomNumber()
    {
        $result="";

        for($i=0;$i<SmsService::random_num;$i++)
        {
            $num = rand(0,9);

            $result = $result.$num;
        }

        return $result;

    }

    /*
     *
     * 手机号验证
     *
     */
    public  static function isVaildMobile($mobile)
    {
        if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$mobile))
        {}



    }

    /*
     * 添加新短信
     *
     */
    public static function addNewSms($mobile,$cap_code,$type)
    {
        $sms = new GlSms();
        $sms->mobile = $mobile;
        $sms->cap_valid=1;
        $sms->cap_code = $cap_code;
        $sms->content = "123";
        $sms->type = $type;
        $sms->create_time = DateUtil::GetCurrentUTCDateTime();
        $sms->expire_time = DateUtil::getNowTimeAfter(MsgConst::SMS_EXPIRE_TIME);
        $r1= $sms->save();

        if (!$r1) {
            throw new Exception("短信发送错误");
        }
    }

    /*
     * 短信验证
     *
     */
    public static function isVaildCode($mobile,$cap_code,$type)
    {
        $smslist = GlSms::model()->findAll("mobile=:mobile and cap_valid=1",
            array(":mobile" => $mobile));

        if(count($smslist)==0)
        {
            throw new Exception("短信验证码错误");
        }

        $sms= $smslist[0];

        //验证码 类型

        if($sms->type != $type)
            throw new Exception("短信验证码类型错误");


        if($sms->cap_code == $cap_code)
        {
            //有无过期

            if($sms->expire_time < DateUtil::getNowTime())
            {
                throw new Exception("短信验证码过期");
            }


        }
        else
        {
            throw new Exception("短信验证码错误");

        }


    }

    /*
     * 短信无效
     *
     */
    public static function invaildMsg($mobile)
    {
        $smslist = GlSms::model()->findAll("mobile=:mobile and cap_valid=1",
            array(":mobile" => $mobile));

        if(count($smslist)>0)
        {
            GlSms::model()->updateAll(array('cap_valid'=>'0','update_time'=>DateUtil::getNowTime()),'mobile=:mobile and cap_valid=1 ',array(':mobile'=>$mobile));
        }

    }


}


?>